package com.controller;

import com.api.CommonResult;
import com.entity.table.ZhongcanTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Zhongcan;
import com.service.ZhongcanService;

import java.io.Serializable;
import java.util.List;

/**
 * 美食展示 中餐 控制层。
 *
 * @author admin
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/admin/zhongcan")
public class ZhongcanController {

//    @GetMapping("")
//    public String zhongcan(){
//        return "client/zhongcan";
//    }
    @Autowired
    private ZhongcanService zhongcanService;

    /**
     * 添加美食展示 中餐。
     *
     * @param zhongcan 美食展示 中餐
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody Zhongcan zhongcan) {
//        return zhongcanService.save(zhongcan);
//    }

    /**
     * 根据主键删除美食展示 中餐。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return zhongcanService.removeById(id);
    }

    /**
     * 根据主键更新美食展示 中餐。
     *
     * @param zhongcan 美食展示 中餐
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody Zhongcan zhongcan) {
//        return zhongcanService.updateById(zhongcan);
//    }

    /**
     * 查询所有美食展示 中餐。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<Zhongcan> list() {
//        return zhongcanService.list();
//    }

    /**
     * 根据美食展示 中餐主键获取详细信息。
     *
     * @param id 美食展示 中餐主键
     * @return 美食展示 中餐详情
     */
    @GetMapping("getInfo/{id}")
    public Zhongcan getInfo(@PathVariable Serializable id) {
        return zhongcanService.getById(id);
    }

    /**
     * 分页查询美食展示 中餐。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Zhongcan> page(Page<Zhongcan> page) {
        return zhongcanService.page(page);
    }

    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Zhongcan zhongcan) {
        try {
            boolean r = zhongcanService.save(zhongcan);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = zhongcan.getZcName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update( Zhongcan zhongcan) {
        try {
            boolean r =zhongcanService.updateById(zhongcan);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = zhongcan.getZcName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "/List")
    public String List(ModelMap model, @RequestParam(value = "ZcName", defaultValue ="")String ZcName,
                           @RequestParam(value = "p", defaultValue = "1")int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
        QueryWrapper queryWrapper = QueryWrapper.create().where(ZhongcanTableDef.ZHONGCAN.ZC_NAME.like(ZcName));
        Page<Zhongcan> page = new Page<>();
        page.setPageSize(pageSize);
        page.setPageNumber(p);
        Page<Zhongcan> pageInfo = zhongcanService.page(page,queryWrapper);
        model.addAttribute("page",pageInfo);
        model.addAttribute("total",pageInfo.getTotalRow());
        return "back/zhongcan-list";
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try{
            boolean r = zhongcanService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    } @GetMapping(value = "addView")
    public String addUserView(){return "back/zhongcan-add";
    }
    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        Zhongcan zhongcan = zhongcanService.getById(id);
        model.addAttribute("zhongcan",zhongcan);
        return "back/zhongcan-edit";
    }
}
